package com.egg.auth.config;

import com.egg.auth.service.dept.IDeptService;
import com.egg.auth.service.login.LoginHelper;
import com.egg.auth.service.role.IRoleService;
import com.egg.auth.service.user.IUserService;
import com.egg.auth.utils.PasswordUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * 初始化 配置类
 */
@Slf4j
@Configuration
public class InitBeanConfig {


    /**
     * 装配
     *
     * @return
     */
    @Bean
    LoginHelper initLoginHelper(
            IUserService userService
            , IRoleService roleService
            , IDeptService deptService
    ) {
        if (null == deptService) {
            log.error("初始化失败 {} 原因 {}", LoginHelper.class.getName(), IDeptService.class.getName() + "为null");
            return new LoginHelper();
        }
        if (null == roleService) {
            log.error("初始化失败 {} 原因 {}", LoginHelper.class.getName(), IRoleService.class.getName() + "为null");
            return new LoginHelper();
        }
        if (null == userService) {
            log.error("初始化失败 {} 原因 {}", LoginHelper.class.getName(), IUserService.class.getName() + "为null");
            return new LoginHelper();
        }

        LoginHelper init = new LoginHelper();
        init.setUserServiceBean(userService);
        init.setRoleServiceBean(roleService);
        init.setDeptServiceBean(deptService);
        log.info("初始化成功 {}", LoginHelper.class.getName());
        return init;
    }

    @Bean
    PasswordUtil initPasswordUtil() {
        PasswordUtil passwordUtil = new PasswordUtil();
        /**
         * 强散列哈希加密实现
         */
        passwordUtil.setBCryptPasswordEncoder(new BCryptPasswordEncoder());
        return passwordUtil;
    }


}